﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using I = IML.Shared;

namespace IML
{
    public partial class Result : Form
    {
        I.ImageCompilerRequest _request;
        public Result()
        {
            InitializeComponent();
            this.ResizeRedraw = true;

            SetSize(900, 600);
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            this.Text = String.Format("{0} x {1}", panel1.Width, panel1.Height);
            try
            {
                e.Graphics.Flush();
                e.Graphics.Clear(Color.Black);
                e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                new ImageCompiler().DrawCanvas(_request, e.Graphics);
            }
            catch (ImageCompilationException iex)
            {
                MessageBox.Show(iex.Message, "Image Compilation Error");
            }
        }

        public void SetSize(int width, int height)
        {
            int deltaX = this.Width - panel1.Width;
            int deltaY = this.Height - panel1.Height;

            this.Width = width + deltaX;
            this.Height = height + deltaY;
        }
        

        private void panel1_Resize(object sender, EventArgs e)
        {
            if (_request == null) { return;  }
            _request.CanvasHeight = panel1.Height;
            _request.CanvasWidth = panel1.Width;
            panel1.Invalidate();
        }

        public void CompileImage(I.ImageCompilerRequest request)
        {
            this.SetSize(request.CanvasWidth, request.CanvasHeight);
            _request = request;
            this.Show();
        }
    }
}
